<template>
  <div class="popupSelect-container">
    <div class="el-select" @click="openDialog">
      <el-input
        placeholder="请选择应用授权"
        v-model="title"
        readonly
        :validate-event="false"
        @mouseenter.native="inputHovering = true"
        @mouseleave.native="inputHovering = false"
      >
        <template slot="suffix">
          <i
            v-show="!showClose"
            :class="['el-select__caret', 'el-input__icon', 'el-icon-arrow-up']"
          ></i>
          <i
            v-if="showClose"
            class="el-select__caret el-input__icon el-icon-circle-close"
            @click.stop="clear"
          ></i>
        </template>
      </el-input>
    </div>
    <el-dialog
      title="应用"
      :close-on-click-modal="false"
      :visible.sync="visible"
      class="jsbos-dialog jsbos-dialog_center jsbos-dialog-tree-select"
      lock-scroll
      append-to-body
      width="600px"
    >
      <div class="jsbos-common-layout">
        <div class="jsbos-common-layout-center">
          <el-row class="jsbos-common-search-box" :gutter="16">
            <el-form @submit.native.prevent>
              <el-col :span="10">
                <el-form-item label="关键词">
                  <el-input
                    v-model="listQuery.keyword"
                    placeholder="请输入关键词查询"
                    clearable
                    @keyup.enter.native="search()"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="search()"
                    >查询</el-button
                  >
                  <el-button icon="el-icon-refresh-right" @click="reset()"
                    >重置</el-button
                  >
                </el-form-item>
              </el-col>
            </el-form>
            <div class="jsbos-common-search-box-right">
              <el-tooltip effect="dark" content="刷新" placement="top">
                <el-link
                  icon="icon-ym icon-ym-Refresh jsbos-common-head-icon"
                  :underline="false"
                  @click="initData()"
                />
              </el-tooltip>
            </div>
          </el-row>
          <div class="jsbos-common-layout-main jsbos-flex-main">
            <jsbos-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="list"
              :border="false"
              highlight-current-row
              @row-click="rowClick"
              :hasNO="false"
              :hasC="true"
              @selection-change="handleSelectionChange"
            >
              <!-- <el-table-column width="35">
                <template slot-scope="scope">
                  <el-radio :label="scope.row.id" v-model="checked"
                    >&nbsp;</el-radio
                  >
                </template>
              </el-table-column> -->
              <el-table-column
                type="index"
                width="50"
                label="序号"
                :align="'center'"
              />
              <el-table-column
                prop="fullName"
                label="名称"
                show-overflow-tooltip
              />
              <el-table-column prop="enCode" label="编码" />
            </jsbos-table>
            <pagination
              :total="total"
              :page.sync="listQuery.currentPage"
              :limit.sync="listQuery.pageSize"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">{{
          $t("common.cancelButton")
        }}</el-button>
        <el-button type="primary" @click="select()" size="small"
          >{{ $t("common.confirmButton") }}
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSelector } from "@/views/message/api/mcAccountConfig";
export default {
  name: "McBusinessSelect",
  props: {
    value: {
      default: "",
    },
    accountConfigIds: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "",
    },
    clearable: {
      type: Boolean,
      default: true,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    menuId: {
      type: String,
      default: "",
    },
    messageType: {
      type: String,
      default: "",
    },
  },
  model: {
    prop: "value",
    event: "input",
  },
  data() {
    return {
      list: [],
      innerValue: "",
      query: {
        category: this.messageType,
      },
      listQuery: {
        keyword: "",
        enabledMark: "1", // 0无效，1有效
        currentPage: 1,
        pageSize: 20,
      },
      total: 0,
      checked: [],
      checkedRow: [],
      listLoading: false,
      inputHovering: false,
      visible: false,
      multipleSelection: [],
    };
  },
  computed: {
    showClose() {
      let hasValue =
        this.value !== undefined && this.value !== null && this.value !== "";
      let criteria =
        this.clearable && !this.disabled && this.inputHovering && hasValue;
      return criteria;
    },
  },
  watch: {
    messageType() {
      this.query.category = this.messageType;
    },
  },
  methods: {
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      getSelector(_query)
        .then((res) => {
          this.list = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
          this.$nextTick(() => {
            if (this.checked) {
              this.checked.forEach((item) => {
                let _row = this.list.find(function (elem) {
                  return elem.id == item;
                });
                this.$refs.multipleTable.$refs.JSBOSTable.toggleRowSelection(
                  _row,
                  true
                );
                this.checkedRow.push(_row);
              });
            }
          });
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    openDialog() {
      if (this.$isEmpty(this.messageType)) {
        return this.$message.warning("请选择消息类型");
      }

      if (this.disabled) return;
      this.checked = this.accountConfigIds;
      this.visible = true;
      this.reset();
    },
    reset() {
      this.listQuery.keyword = "";
      this.search();
    },
    search() {
      this.listQuery.currentPage = 1;
      this.listQuery.pageSize = 20;
      this.initData();
    },
    clear() {
      this.$refs.multipleTable.$refs.JSBOSTable.clearSelection();
      this.checked = [];
      this.checkedRow = [];
      this.$emit("input", this.checked);
      this.$emit("change", this.checked, this.checkedRow);
    },
    select() {
      this.$emit("table-change", this.multipleSelection);
      this.visible = false;
    },
    rowClick(row) {
      if (this.checked) {
        const _idx = this.checked.indexOf(row.id);
        if (_idx == -1) {
          // 选中
          this.checked.push(row.id);
          this.checkedRow.push(row);
          this.$refs.multipleTable.$refs.JSBOSTable.toggleRowSelection(
            row,
            true
          );
        } else {
          // 取消选中
          this.checked.splice(_idx, 1);
          this.checked.splice(_idx, 1);
          this.$refs.multipleTable.$refs.JSBOSTable.toggleRowSelection(
            row,
            false
          );
        }
      } else {
        this.checked = [];
        this.checkedRow = [];
      }
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
  },
};
</script>
<style lang="scss" scoped>
>>> .el-dialog__body {
  max-height: 70vh;
  padding: 0 0 10px !important;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  common-search-box {
    margin-bottom: 0;
    common-search-box-right {
      padding: 10px 10px 0 0;
    }
  }
}
</style>
